home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume3 / rmsecure < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  5.3 KB

  1. From: DIDELOT Andre <talcott!seismo!mcvax!cui!andre>
  2. Subject: rmsecure - source for a safe "rm"
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 103
  7. Submitted by: DIDELOT Andre <talcott!seismo!mcvax!cui!andre>
  8.  
  9.  
  10. Below is the presentation and the code of a command which replace "rm";
  11. it works exactly the same, except that it just asks for confirmation when
  12. its arguments use some wild cards.
  13.  
  14. [ This ONLY works with csh, because it uses aliases, and it may only work
  15.   with the BSD4.2 or better csh      - John P. Nelson, moderator mod.sources
  16. ]
  17.  
  18. I wrote it long ago, after having had some bad experiments with conjonction
  19. of misstyping and wild cards in the use of the usual "rm" command.
  20. These accidents turned sometimes to drama, especially when I was learning
  21. the (difficult?:-) job of the superuser.
  22. Months along, other users too experimented such personnal disasters and the
  23. safe "rm" gained popularity on our site. We have now used it without problems
  24. over a year.
  25.  
  26. I would be pleased if it could be of some interest for other (super)users.
  27.  
  28.     Andre DIDELOT                CHUNET: andre@cui.unige.chunet
  29.  
  30. MAIL:    Centre Universitaire d'Informatique    UUCP:    mcvax!cernvax!cui!andre
  31.     Universite de Geneve                mcvax!cernvax!cui!root
  32.     Rue du General Dufour 24
  33.     CH - 1211 GENEVE 4            BITNET: DIDELOT@CGEUGE51
  34.     SWITZERLAND                    SYSTEM@CGEUGE51
  35.  
  36.  
  37. ===============================================================================
  38. #! /bin/sh
  39. # This is a shell archive, meaning:
  40. # 1. Remove everything above the #! /bin/sh line.
  41. # 2. Save the resulting text in a file.
  42. # 3. Execute the file with /bin/sh (not csh) to create the files:
  43. #    rmsecure.doc
  44. #    rmsecure.csh
  45. # This archive created: Wed Feb  5 18:39:13 1986
  46. export PATH; PATH=/bin:$PATH
  47. echo shar: extracting "'rmsecure.doc'" '(1893 characters)'
  48. if test -f 'rmsecure.doc'
  49. then
  50.     echo shar: will not over-write existing file "'rmsecure.doc'"
  51. else
  52. cat << \SHAR_EOF > 'rmsecure.doc'
  53. Adding security when removing files:
  54. ------------------------------------
  55.  
  56. DESCRIPTION:
  57. ------------
  58. With the standard "rm", no confirmation is asked when using wild cards;
  59. you surely once experienced that this may be really catastrophic.
  60. If not, think about a misstyping like "rm * BAK" instead of "rm *BAK" !!!
  61.  
  62. A new command, "rmsecure", may prevent this by asking for a confirmation
  63. before removing any file whose name issued from a wild card expansion.
  64.  
  65. This command works a bit slower than the usual "rm", but is really safer !
  66.  
  67.  
  68. USE:
  69. ---
  70. To use it, you MUST create a special alias like this one:
  71.  
  72.     alias rm '(set noglob; rmsecure \!*)'
  73.  
  74. in order to prevent former wild cards expansion by the shell.
  75.  
  76. When used, any list of files resulting from a wild card expansion will be
  77. asked globally for confirmation prior to deletion; if answer is negative,
  78. each file in the expanded list will be prompted separatly, like the "-i"
  79. option of usual "rm".
  80.  
  81. As reel deletion take place only just before completion of the command,
  82. this one can be safely interrupted at any time if necessary; in this case,
  83. no file will be removed.
  84.  
  85.  
  86. WARNING:
  87. --------
  88. Files whose name is completely specified won't be asked for confirmation,
  89. even if they appear between other arguments using wild cards.
  90.  
  91.  
  92. LIMITS:
  93. -------
  94.  - as all expanded file names are concatened within one unique string,
  95.    command can fail due to limitation of shell variable length.
  96.    To overcome this, just select narrowed range.
  97.  
  98.  - due to successive evaluations, any quotes or double quotes will be lost;
  99.    removing files with "strange" names should be done with the standard "rm"
  100.    command using the "-i" option with a "*" wild card.
  101.  
  102.  - the following version is running perfectly with Cshell of 4.2 bsd;
  103.    problems were encountered trying to run it on Unisoft System III,
  104.    probably due to some limitation of the Cshell implemented.
  105.  
  106. SHAR_EOF
  107. if test 1893 -ne "`wc -c < 'rmsecure.doc'`"
  108. then
  109.     echo shar: error transmitting "'rmsecure.doc'" '(should have been 1893 characters)'
  110. fi
  111. fi
  112. echo shar: extracting "'rmsecure.csh'" '(1066 characters)'
  113. if test -f 'rmsecure.csh'
  114. then
  115.     echo shar: will not over-write existing file "'rmsecure.csh'"
  116. else
  117. cat << \SHAR_EOF > 'rmsecure.csh'
  118. #! /bin/csh -f
  119. # csh script ; secure remove of files
  120. onintr out
  121. set interact
  122. set force
  123. set recur
  124. set minus
  125. while ("$1" == "-" || "$1" =~ -[f,i,r])
  126.     switch ($1)
  127.     case -i: 
  128.         set interact=-i
  129.         shift
  130.         breaksw
  131.     case -f: 
  132.         set force=-f
  133.         shift
  134.         breaksw
  135.     case -r: 
  136.         set recur=-r
  137.         shift
  138.         breaksw
  139.     case  -: 
  140.         set minus=-
  141.         shift
  142.         break
  143.     endsw
  144. end
  145. if ($interact == -i) then
  146.     /bin/rm -i $force $recur $minus $*
  147.     exit($status)
  148. endif
  149. set f1=`echo $0`
  150. set f1=$f1:t" : remove"
  151. set r
  152. while ("$1" != '')
  153.     set b=`echo $1`
  154.     if ("$b" != '') then
  155.     set noglob
  156.     set a=$1
  157.     unset noglob
  158.     if ("$a" == "$b") then
  159.         set r="$r $b"
  160.     else
  161.         if ($#b == 1) then
  162.         echo -n $f1 $b '? '
  163.         if ( $< == "y" ) then
  164.             set r="$r $b"
  165.         endif
  166.         else
  167.         echo -n $f1 $b '? '| fold
  168.         if ( $< == "y" ) then
  169.             set r="$r $b"
  170.         else
  171.             foreach n ($b)
  172.             echo -n $f1 $n '? '
  173.             if ( $< == "y" ) then
  174.                 set r="$r $n"
  175.             endif
  176.             end
  177.         endif
  178.         endif
  179.     endif
  180.     endif
  181.     shift
  182. end
  183. /bin/rm $force $recur $minus $r
  184. out: 
  185. exit(0)
  186. SHAR_EOF
  187. if test 1066 -ne "`wc -c < 'rmsecure.csh'`"
  188. then
  189.     echo shar: error transmitting "'rmsecure.csh'" '(should have been 1066 characters)'
  190. fi
  191. fi
  192. exit 0
  193. #    End of shell archive
  194.